<?php
/**
 * 功能描述：	日志管理
 * @datetime	2022-08-28
 * @version   	v2.3
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_SyslogServe extends Model {

    /**
     * Notes:添加登录日志
     * User: ZhuHaili
     */
    public function addLoginLog($userInfo){
        if(!isset($userInfo['id'])){
            return false;
        }
        if(!isset($userInfo['nickname'])){
            return false;
        }
        if(!isset($userInfo['mobile'])){
            return false;
        }
        if(!isset($userInfo['email'])){
            return false;
        }
        $data['user_id'] = $userInfo['id'];
        $data['title'] = $userInfo['title'] ? $userInfo['title'] : $userInfo['nickname'];
        $data['user_mobile'] = $userInfo['mobile'];
        $data['user_email'] = $userInfo['email'];
        $data['ip'] = clientIP();
        $data['status'] = '1';
        $data['createdate'] = date('Y-m-d H:i:s');
        $result = $this->conn('mysql')->addData('syslog_login', $data);
        if(!$result){
            return false;
        }
        $loginM = new Admin_SyslogModel();
        $loginM->updateUserLoginInfo($userInfo['id']);
        return true;
    }

    /**
     * Notes:添加操作日志
     * User: ZhuHaili
     * Date: 2022/8/31
     */
    public function addOperateLog($controller, $action, $menuId, $content = ''){
        // 操作日志白名单过滤
        $whiteLog = $GLOBALS['CONFIG']['white_log'];
        $classAction = $controller.'_'.$action;
        if(in_array($classAction, $whiteLog)){
            return true;
        }

        // 获得菜单名字
        $classInfo = array();
        if($menuId){
            $classInfo = $this->conn('mysql')->findData('sysmenu', array('id'=>$menuId));
        }
        if(empty($classInfo)){
            $classInfo = $this->conn('mysql')->findData('sysmenu', array('setpath'=>$controller));
        }
        $className = isset($classInfo['menuname']) ? $classInfo['menuname'] : '';

        // 判断类型
        $typeeArr = BaseData::$logActTypeNoArr;
        $type = isset($typeeArr[$action]) ? $typeeArr[$action] : '7';
        $subFlag = Fun::input('subFlag');
        $type = $subFlag == 'Y' ? $type : '1';

        $loginUserInfo = SYS_SESSIONS_GET(LOGIN_SESSION_FIX.'alogin_info');
        $userId = '';
        if(isset($loginUserInfo['id']) && $loginUserInfo['id']){
            $userId = $loginUserInfo['id'];
        }
        $nickname = '';
        if(isset($loginUserInfo['nickname']) && $loginUserInfo['nickname']){
            $nickname = $loginUserInfo['nickname'];
        }
        // 记录日志
        $data['user_id'] = $userId;
        $data['title'] = $nickname;
        $data['controller'] = $controller;
        $data['actionfun'] = $action;
        $data['menu_id'] = $menuId;
        $data['menu_name'] = $className;
        $data['content'] = $content;
        $data['ip'] = clientIP();
        $data['types'] = $type;
        $data['status'] = '1';
        $data['createdate'] = date('Y-m-d H:i:s');
        $this->conn('mysql')->addData('syslog_act', $data);
        return true;
    }
}